package flc.ast.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import VideoHandle.EpAudio;
import VideoHandle.EpEditor;
import VideoHandle.OnEditorListener;
import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.adapter.AudioMergerAdapter;
import flc.ast.databinding.ActivityAudioSpliceBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.media.audio.AudioPlayerImpl;
import stark.common.basic.media.audio.IAudioPlayer;
import stark.common.basic.utils.FileP2pUtil;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.TimeUtil;
import stark.common.basic.utils.WorkPathUtil;

public class AudioSpliceActivity extends BaseAc<ActivityAudioSpliceBinding> {

    public static List<String> mAddList = new ArrayList<>();
    private AudioMergerAdapter mAudioMergeAdapter;
    private AudioPlayerImpl mAudioPlayer;
    private AudioPlayerImpl mCurAudioPlayer;

    @Override
    protected int onCreate() {
        return R.layout.activity_audio_splice;
    }

    @Override
    protected void initView() {
        mDataBinding.ivAudioSpliceBack.setOnClickListener(v -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.tvSplice.setOnClickListener(this);

        mAudioPlayer = new AudioPlayerImpl();
        mCurAudioPlayer = new AudioPlayerImpl();

        mAudioMergeAdapter = new AudioMergerAdapter(mAudioPlayer);
        mAudioMergeAdapter.mCurAudioPlayer = mCurAudioPlayer;
        mDataBinding.lvAudio.setAdapter(mAudioMergeAdapter);
        for (String s : mAddList) {
            mAudioMergeAdapter.addAudioPath(s);
        }
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.tvSplice) {//立即拼接
            splice();
        }
    }

    @Override
    protected void initData() {

    }

    private void splice() {
        //音频拼接
        List<EpAudio> audioList = new ArrayList<>();
        for (int i = 0; i < mAudioMergeAdapter.getCount(); i++) {
            audioList.add(new EpAudio(mAudioMergeAdapter.getHolderList().get(i).mBindPath));
        }
        String path = FileUtil.generateFilePath(Content.myTemp, ".mp3");
        showDialog(getString(R.string.audio_splice_ing));
        RxUtil.create(new RxUtil.Callback<String>() {
            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                EpEditor.audioConcat(audioList, path, new OnEditorListener() {
                    @Override
                    public void onSuccess() {
                        emitter.onNext(path);
                    }

                    @Override
                    public void onFailure() {
                        dismissDialog();
                    }

                    @Override
                    public void onProgress(float progress) {
                    }
                });
            }

            @Override
            public void accept(String audioOutPath) {
                if (!TextUtils.isEmpty(audioOutPath)) {
                    mDataBinding.tvSplice.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            dismissDialog();
                            SpliceResultActivity.audioPath = audioOutPath;
                            startActivity(SpliceResultActivity.class);
                        }
                    }, 1000);
                }
            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mAudioPlayer != null) {
            mAudioPlayer.pause();
        }

        if (mCurAudioPlayer != null) {
            mCurAudioPlayer.pause();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mAudioPlayer != null) {
            mAudioPlayer.stop();
        }
        if (mCurAudioPlayer != null) {
            mCurAudioPlayer.stop();
        }
        FileUtils.deleteAllInDir(PathUtils.getFilesPathExternalFirst() + Content.myTemp);
        FileUtils.deleteAllInDir(PathUtils.getFilesPathExternalFirst() + WorkPathUtil.WORK_AUDIO_DIR);
    }

}